<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\model\Reason as reasonModel;//学生信息模型
use app\admin\model\AdminMenu;
use app\admin\controller\User;
class Reason extends User
{
    /**
     * 学生积分列表
     * @return [type] [description]
     */
    public function index()
    {
        //实例化积分模型
        $model = new reasonModel();
        $sname='';
        $post = $this->request->post();
        if (isset($post['xid']) and $post['xid'] > 0) {
            $where['xid'] = $post['xid'];
        }
        if (isset($post['cid']) and $post['cid'] > 0) {
            $where['cid'] = $post['cid'];
        }
        if (isset($post['sname']) and !empty($post['sname'])) {
            $where['sname'] = ['like', '%' . $post['sname'] . '%'];
            $sname=$post['sname'];
        }
        $reason = empty($where) ? $model->order('reid asc')->paginate(4) : $model->where($where)->order('reid asc')->paginate(4);

      // print_r($reason);

        $stu=Db::name("stu")->select();
        $school=Db::name("school")->select();
        $cla=Db::name("cla")->select();
        $this->assign('school',$school);
        $this->assign('stu',$stu);
        $this->assign('reason',$reason);
        $this->assign('cla',$cla);
        $this->assign('sname',$sname);
       return $this->fetch();
    }
    /**
     * 积分的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取积分表id
        $reid = $this->request->has('reid') ? $this->request->param('reid', 0, 'intval') : 0;
        $model = new reasonModel();
        if($reid > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['cid', 'require', '班级名称不能为空'],
                    ['xid', 'require','必须选择学校'],
                    ['sid', 'require','姓名不能为空'],
                    ['rid', 'require','原因不能为空'],
                    ['fid', 'require','分数不能为空']
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证学生积分是否存在
               // $xname = $model->where(['cid'=>$post['cid'],'xid'=>$post['xid'],'sid'=>$post['sid'],'rid'=>$post['rid'],'fid'=>$post['fid']])->select();
                if(!empty($xname)) {
                    return $this->error('提交失败：已经存在该学生积分');
                }
                if(false == $model->allowField(true)->save($post,['reid'=>$reid])) {
                    return $this->error('修改失败');
                } else {
                    //addlog($model->id);//写入日志
                    return $this->success('修改学生积分信息成功','admin/stu/index');
                }
            } else {
                //非提交操作
                $info['reason'] = $model->where('reid',$reid)->find();
                $info['stu']=Db::name("stu")->select();
                $info['school']=Db::name("school")->select();
                $info['cla']=Db::name("cla")->select();
                $info['record']=Db::name("record")->select();
                $info['fenshu']=Db::name("fenshu")->select();
                $this->assign('info',$info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['cid', 'require','班级名称不能为空'],
                    ['xid', 'require','必须选择学校'],
                    ['sid', 'require','姓名不能为空'],
                    ['rid', 'require','原因不能为空'],
                    ['fid', 'require','分数不能为空']
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证该学生积分信息是否存在
//                $name = $model->where(['xid'=>$post['xid'],'cid'=>$post['cid'],'sname'=>$post['sname']])->select();
//                if(!empty($name)) {
//                    return $this->error('提交失败：已经存在该学生');
//                }
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加学生积分失败');
                } else {
                    //addlog($model->id);//写入日志
                    return $this->success('添加学生积分成功','admin/reason/index');
                }
            } else {
                //非提交操作
                $info['stu']=Db::name("stu")->select();
                $info['school']=Db::name("school")->select();
                $info['cla']=Db::name("cla")->select();
                $info['record']=Db::name("record")->select();
                $info['fenshu']=Db::name("fenshu")->select();
                $this->assign('info',$info);
                return $this->fetch();
            }
        }
    }
    /**
     * 学生积分删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('reason')->where('reid',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                //addlog($id);//写入日志
                return $this->success('删除成功','admin/reason/index');
            }
        }
    }
    //联动调用方法
    public function cla()
    {
        if($this->request->isAjax()) {
            $xid = $this->request->has('xid') ? $this->request->param('xid', 0, 'intval') : 0;
            $cla=Db::name("cla")->where('xid',$xid)->select();
            $str='<option>-请选择班级-</option>';
            foreach ($cla as $key => $value){
                $str.='<option value="'.$value['cid'].'">'.$value['cname'].'</option>';
            }
            return $str;
        }
    }
    public function stu()
    {
        if($this->request->isAjax()) {
            $cid = $this->request->has('cid') ? $this->request->param('cid', 0, 'intval') : 0;
            $stu=Db::name("stu")->where('cid',$cid)->select();
            $str='<option>-请选择学生-</option>';
            foreach ($stu as $key => $value){
                $str.='<option value="'.$value['sid'].'">'.$value['sname'].'</option>';
            }
            return $str;
        }
    }
}
?>